<?php

function experts_list_page()
{
    @$output = drupal_render(drupal_get_form('experts_list_form'));
    return $output;
}

function experts_list_form($form, &$form_state)
{
     $form['experts_name'] = array(
        '#type' => 'textfield',
        '#title' => '专家姓名',
        '#default_value' => !empty($form_state['values']['experts_name'])?$form_state['values']['experts_name']:"",
        '#id'=>'search-expert-name',
        '#maxlength' => 20,
    );
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');

    return $form;
}

function experts_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
    global $user;
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '专家姓名', 'class' => ['essential more']),
        array('data' => '通讯地址', 'class' => ['essential more']),
        array('data' => '电话', 'class' => ['essential more']),
        array('data' => '邮箱', 'class' => ['essential more']),
        array('data' => '区域', 'class' => ['essential more']),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select('experts', 'V')->extend('PagerDefault')->extend('TableSort');


    if (!empty($form_state['values']['experts_name'])) {//查询条件
        $query->condition('V.experts_name', '%'.db_like($form_state['values']['experts_name']) . '%', 'LIKE');
    }
    $query->fields('V');
    if(empty($form_state['values']) || empty($form_state['values']['order'])) {
        $query->orderBy('V.timestamp', 'DESC');
    }
    $query->limit(variable_get("list_page_item_number", 5));
    $expertsInfo = $query->orderByHeader($header)->execute();
    $rows = array();
    //表内容
    $n = 1;
    foreach ($expertsInfo as $row) {
        if (nonghua_user_access('experts', 'delete', $row->experts_id, $row->author_uid, 0)) {
            $disabled = ' class="listViewTableCheck" ';
        } else{
            $disabled = ' disabled="true" ';
        }
        $rows[] = array(
            array('data' => "<input type='checkbox'".$disabled." value ='" . $row->experts_id . "' name='" . $row->experts_id . "'/>" . $n++, 'class' => 'integrant essential no'),
            array('data' => '<a href="'.base_path().'experts_work/list?expert_id='.$row->experts_id.'">'.$row->experts_name.'</a>', 'class' => ['essential distributor']),
            array('data' => $row->address, 'class' => ['essential distributor']),
            array('data' => $row->cell, 'class' => ['essential distributor']),
            array('data' => $row->email, 'class' => ['essential distributor']),
            array('data' => $row->region, 'class' => ['essential more']),
            array('data' => '<a href="' . base_path() . 'experts/edit/' . $row->experts_id . '"  data-toggle="modal" target="_self"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
        );
    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0 && (user_access('delete own experts') ||  user_access('delete all experts'))) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
     $output .= '<a href="'.base_path().'experts/export?'.$str.'&experts_name='.$form_state['values']['experts_name'].'" data-toggle="modal" class="btn btn-mini">导出</a>';
    $output .= theme('pager');
    return $output;
}

function experts_delete()
{
    ajax_table_delete('experts', 'experts_id');
}

function experts_export($form,&$form_state){
    $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    $tableheader = array('专家姓名','地址','电话','邮箱','区域','专长描述','职业资格','经历描述');
    $data = get_experts_data($form, $form_state);
    nonghua_php_excel($letter,$tableheader,$data,'专家数据');
}

function get_experts_data($form = '', &$form_state = ''){
    global $user;
    $user_info = _get_user_info($user->uid);
   $query = db_select('experts', 'V');


    if (!empty($_GET['experts_name'])) {//查询条件
        $query->condition('V.experts_name', '%'.db_like($_GET['experts_name']) . '%', 'LIKE');
    }
    $query->fields('V',array('experts_name','address','cell','email','region','description','qualification','comment'));
    $expertsInfo = $query->execute()->fetchAll();
    return $expertsInfo;
}